//
//  TTHMHomeOriginalView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/8.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class TTHMHomeOriginalView: UIView {

    
    var pictureContain:Constraint?
    var stusModel:TTStatuesViewModel?
    {
        didSet{
        
            guard let currenModel = stusModel else {
                return
            }
            
            if let imageUrl = currenModel.statues?.user?.profile_image_url {
         userImageView.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            if let name = currenModel.statues?.user?.screen_name
            {
            screenNameLabel.text = name
            }
            mbrankImageView.image = currenModel.mbrank
            verifiedTypeImageView.image = currenModel.verifiedTypeImageView
            contentLabel.text = currenModel.statues?.text
            sourceLabel.text = currenModel.scoure
            timeLabel.text = currenModel.time
            
            pictureContain?.deactivate()
            //显示pictureColltionView
            if let picUrls = currenModel.statues?.pic_urls, picUrls.count > 0
            {
                pictureColltionView.isHidden = false
                
                 self.snp.makeConstraints({ (make) in
                pictureContain = make.bottom.equalTo(pictureColltionView).offset(magin).constraint
                 })
             
                pictureColltionView.pics = currenModel.statues?.pic_urls
                
            
            }else
            {
                pictureColltionView.isHidden = true

                self.snp.makeConstraints({ (make) in
                    pictureContain = make.bottom.equalTo(contentLabel).offset(magin).constraint
                })
                
            }
            
            
        }
        
    }
    
   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      

        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  用户头像
    private lazy var userImageView: UIImageView = {
        
        let image = UIImage(named: "avatar_default_big")
        
        
        let uiimage:UIImageView = UIImageView(image: image)

        uiimage.layer.cornerRadius = 35/2
        uiimage.layer.masksToBounds = true
        
        return uiimage
    
    }()
    //  认证类型图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenNameLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "哈哈"
        return label
    }()
    
    //  时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        return label
    }()
    
    //  来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "来自: 新浪微博"
        return label
    }()
    //  会员等级图片
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "窗前明月光，疑似地上霜，举头望明月，低头思故乡‘窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡窗前明月光，疑似地上霜，举头望明月，低头思故乡"
        //  多行显示
        label.numberOfLines = 0
        return label
    }()

    // MARK: --懒加载colltionView
    private lazy var pictureColltionView:TTPictureCollection =  TTPictureCollection()
    
    
    
    
    func setUI()
    {
    
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(pictureColltionView)
        
        userImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(magin)
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(magin)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(userImageView)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(magin)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(magin)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(magin)
            make.leading.equalTo(self).offset(magin)
            make.trailing.equalTo(self).offset(-10)
        }
        
     pictureColltionView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(magin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //关键约束
        self.snp.makeConstraints { (make) in
          pictureContain = make.bottom.equalTo(pictureColltionView).offset(magin).constraint
        }
        
    }
    

}
